/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

window.addEvent('domready', function() {    
    //start client
    var client = new ADS_Client({'url' : serverURL}).run();
    //content pipeline (translates any content to JSON)
    var pipeline = new ContentPipeline({
        url : serverURL        
    });
    //retrive periodicaly weather forecast 
    var weatherForecast = function(){
        pipeline.toJSON({
            service   : 'worldweatheronline.com:simple',
            onSuccess : function(responseJSON, responseText){
                $$('.forecastToday .icon').each(function(el){
                    el.set('src', templateDir + "/images/icons/weather/" + responseJSON.data.weather[0].weatherCode + ".png");
                });
                $$('.forecastToday .temperature').each(function(el){
                    el.set('html', responseJSON.data.weather[0].tempMaxC+' °C');
                });
                $$('.forecastTommorow .icon').each(function(el){
                     el.set('src', templateDir + "/images/icons/weather/" + responseJSON.data.weather[1].weatherCode + ".png");
                });
                $$('.forecastTommorow .temperature').each(function(el){
                    el.set('html', responseJSON.data.weather[1].tempMaxC+' °C');
                });
            }
        });
    }
    weatherForecast();
    weatherForecast.periodical(10000);
    //retrive periodicaly RSS feed
    var rssFeed = function(){
        pipeline.toJSON({
            service   : 'idnes.cz',
            onSuccess : function(responseJSON, responseText){
                $('feed').empty();
                fxmq = new Fx.Marquee($('feed'), {
                    duration: 500
                });
                var announce = null;
                Object.each(responseJSON.channel.item, function(item,key){
                    if(announce != null){
                        announce = announce.chain(function(){
                            fxmq.announce({
                                message: arguments[0],
                                delay: 5000,
                                revert: false
                            });
                        }.pass(item.title + '.'));
                    }else{
                        announce = fxmq.announce({
                            message: item.title + '.',
                            delay: 5000,
                            revert: false
                        });
                    }
                });
                announce.chain(function(){
                   rssFeed(); 
                });
            }
        });
    }
    rssFeed.delay(3000);
    //retrive periodicaly exchange rates
    var exchangeRate = function(){
        pipeline.toJSON({
            service   : 'cnb.cz:today',
            onSuccess : function(todayJSON, todayText){
                pipeline.toJSON({
                    service   : 'cnb.cz:yesterday',
                    onSuccess : function(yesterdayJSON, yesterdayText){
                        //alert('ok');
                        //console.log(yesterdayJSON.usd.rate);
                        $$('.exchange .eur .rate').each(function(el){
                            el.set('html', todayJSON.eur.rate);
                        });
                        $$('.exchange .usd .rate').each(function(el){
                            el.set('html', todayJSON.usd.rate);
                        });
                        $$('.exchange .eur .icon').each(function(el){
                            el.set('src', templateDir + "/images/icons/currency/" + (todayJSON.eur.rate < yesterdayJSON.eur.rate?'down':'up') + ".png");
                        });
                        $$('.exchange .usd .icon').each(function(el){
                            el.set('src', templateDir + "/images/icons/currency/" + (todayJSON.usd.rate < yesterdayJSON.usd.rate?'down':'up') + ".png");
                        });
                    }
                });
            }
        });
    }
    exchangeRate.delay(3000);
    exchangeRate.periodical(10000);
    //clocks
    var clocks = new Clocks({element: 'clocks'});
    clocks.showTime.periodical(1000);
    //nameday
    (function(){
        $('nameday').set('html', getTodayNameday());
        $('date').set('html', new Date().format('%d.%m.%Y'));
    }).periodical(1000);
    

});


